﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Commands.Data;
namespace ZLCMS.Web.SystemManage.Basics.ProductSKU
{
    public partial class SelectProductBatchAdd : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        public string _name;
        public string _productNo;
        public string _productId;
        public string _userAccount;
        public string _productKind;

        public string _parentProductNo;



        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化全局变量值
            _name = Request["name"] == null ? "" : Request["name"];
            _productNo = Request["pno"] == null ? "" : Request["pno"];
            _productId = Request["pid"] == null ? "" : Request["pid"];
            _parentProductNo = Request["parentProductNo"] == null ? "" : Request["parentProductNo"];
            _productKind = Request["productkind"] == null ? "" : Request["productkind"];
            _userAccount = UserKit.GetUserAccountFailLogin();

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };


            //填充网格数据
            BindList(_framePage.PageIndex, _framePage.PageSize);
        }


        #region 填充网格数据 + protected void BindList(int pageIndex, int pageSize)
        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                // ICriteria criteria = session.CreateCriteria<ShopSkuProduct>().CreateCriteria("ShopProductInfo");
                // SetupCondition(ref session);
                string sql = @"select product.PRODUCT_NO as ProductNo 
                 ,product.MARKET_PRICE as  MarketPrice,product.SALE_PRICE as SalePrice,
                   product.PRODUCT_NAME  ProductName ,product.THUMBNAIL_IMG ThumbnailImg from shop_product_info  product 
                    where  product.KIND = 'shop' and product.IS_LIMIT_TIME = '0' and product.status =  '1'";
                ISQLQuery sqlQuery = SetupCondition(sql, session);
                IList<SelectRelateProductData> skuProductList = sqlQuery.SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize)
                     .SetResultTransformer(new ObjectResultTransformer(typeof(SelectRelateProductData)))
               .List<SelectRelateProductData>();




                grid.DataSource = skuProductList;
                grid.DataBind();
            }
        }
        #endregion

        #region 填充查询条件 + private void SetupCondition(ref ICriteria criteria)
        /// <summary>
        /// 填充查询条件
        /// </summary>
        /// <param name="criteria"></param>
        private ISQLQuery SetupCondition(string sql, ISession session)
        {
            if (_name.Trim() != "")
            {
                sql = sql + " and product.PRODUCT_NAME like :name";
            }

            if (_productNo.Trim() != "")
            {
                sql = sql + " and product.PRODUCT_NO like :productNo";
            }
            if (_parentProductNo.Trim() != "")
            {
                sql = sql + " and product.PRODUCT_NO <> :parentProductNo";
            }
            ISQLQuery sqlQuery = session.CreateSQLQuery(sql);
            if (_name.Trim() != "")
            {
                sqlQuery.SetParameter("name", "%" + _name.Trim() + "%");
            }

            if (_productNo.Trim() != "")
            {
                sqlQuery.SetParameter("productNo", "%" + _productNo.Trim() + "%");
            }
            if (_parentProductNo.Trim() != "")
            {
                sqlQuery.SetParameter("parentProductNo", "%" + _productNo.Trim() + "%");
            }
            return sqlQuery;
        }
        #endregion

        #region 查询数据总数 + protected int GetListCount()
        /// <summary>
        /// 查询数据总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                string sql = @"select count(1)  from shop_product_info  product INNER JOIN shop_sku_product sku
                 on product.PRODUCT_NO = sku.PRODUCT_NO 
                    where product.STATUS = '1' and product.KIND = 'shop' and product.IS_LIMIT_TIME = '0'";
                ISQLQuery sqlQuery = SetupCondition(sql, session);
                int totalCounts = Convert.ToInt32(sqlQuery.UniqueResult());
                return totalCounts;
            }
        }
        #endregion

        protected string GetPicture(string path)
        {
            return path + ".thumb" + System.IO.Path.GetExtension(path);
        }




        public string GetSkuItemHtml(string paramCode)
        {
            List<SKUGroup> groupItems = Newtonsoft.Json.JsonConvert.DeserializeObject<List<SKUGroup>>(paramCode);
            string skuItemHtml = "";
            foreach (SKUGroup item in groupItems)
            {
                skuItemHtml += item.GroupName + ":" + item.ItemName;
            }
            return skuItemHtml;
        }
    }
}